﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JavaDevToys.Rcl.Models
{
    public class ColumnInfo
    {
        public string ColumnName { get; set; }
        public string DataType { get; set; }
        public string ColumnComment { get; set; }
        public bool IsPrimaryKey { get; set; }
        public int? Precision { get; set; }
        public int? Scale { get; set; }
        public string JavaType
        {
            get
            {
                return MapSqlTypeToJavaType(DataType, Precision, Scale);
            }
        }
        public string JavaName
        {
            get
            {
                return ToCamelCase(ColumnName);
            }
        }
        // 转换为驼峰命名法
        private string ToCamelCase(string name)
        {
            if (string.IsNullOrEmpty(name)) return name;

            // 处理下划线命名，如 user_name -> userName
            StringBuilder sb = new StringBuilder();
            bool nextUpper = false;
            foreach (char c in name)
            {
                if (c == '_')
                {
                    nextUpper = true;
                }
                else
                {
                    sb.Append(nextUpper ? char.ToUpper(c) : char.ToLower(c));
                    nextUpper = false;
                }
            }
            return sb.ToString();
        }

        // 转换为Pascal命名法
        private string ToPascalCase(string name)
        {
            if (string.IsNullOrEmpty(name)) return name;

            string camel = ToCamelCase(name);
            return char.ToUpper(camel[0]) + camel.Substring(1);
        }

        // 数据类型映射
        // 更新 MapSqlTypeToJavaType 方法以接收 Precision 和 Scale
        private string MapSqlTypeToJavaType(string sqlType, int? precision, int? scale)
        {
            switch (sqlType.ToLower())
            {
                case "int":
                    return "Integer";
                case "bigint":
                    return "Long";
                case "smallint":
                    return "Short";
                case "tinyint":
                    return "Byte";
                case "bit":
                    return "Boolean";
                case "decimal":
                case "numeric":
                case "money":
                case "smallmoney":
                    // 根据 precision 和 scale 决定映射为 Long 或 BigDecimal
                    if (sqlType.ToLower() == "numeric" && precision == 19 && scale == 0)
                    {
                        return "Long";
                    }
                    if (scale == 0)
                        return "Integer";
                    return "BigDecimal";
                case "float":
                    return "Double";
                case "real":
                    return "Float";
                case "date":
                case "datetime":
                case "datetime2":
                case "smalldatetime":
                    return "Date";
                case "time":
                    return "Time";
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "text":
                case "ntext":
                    return "String";
                case "binary":
                case "varbinary":
                case "image":
                    return "byte[]";
                default:
                    return "String"; // 默认转换为String类型
            }
        }
    }
}
